package com.selfdriving.view;


import java.util.List;

import com.selfdriving.R;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.Home;
import com.selfdriving.utils.PhotoUtil;

import net.tsz.afinal.FinalBitmap;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;
/**
 * ViewFlipper的公共类
 * 
 * @author Administrator
 */
public class ViewFlipperView implements OnGestureListener, OnTouchListener
{
	public Context context;
	public ViewFlipper viewFlipper;
	public FinalBitmap fb;
	private final int SHOW_NEXT = 0011;
	private static final int FLING_MIN_DISTANCE = 20;
	private static final int FLING_MIN_VELOCITY = 0;
	private boolean showNext = true;
	private int currentPage = 0;
	public ImageView RadioImg[];
	private LinearLayout RadioImages;
	private GestureDetector mGestureDetector;
	private ImageView imageView; // ViewFlipper的图片对象
	private ImageView imageViews[];// ViewFlipper的图片数组
	private boolean isRun = true;
	private int key = -1;// 辨别是否点击viewFlipper下滑
	private String imgPath[] = null;
	private List<? extends Entity> datas = null;

	/** 图片集合形式获取ViewFlipper **/
	public ViewFlipperView(Context context, LinearLayout RadioImages, int key,
			List<? extends Entity> datas)
	{
		// TODO Auto-generated constructor stub
		this.context = context;
		this.RadioImages = RadioImages;
		this.key = key;
		this.datas = datas;
		if (RadioImages.getChildCount() > 0)
		{// 清除过期点点
			RadioImages.removeAllViews();
		}
		if (datas.size() == 0)
		{
			imgPath = new String[1];
		} else
		{
			imgPath = new String[datas.size()];
		}
		initUI(key);
		getImages();
		getRadioImages();
		isRun();
	}

	/** 图片数组形式获取ViewFlipper **/
	public ViewFlipperView(Context context, LinearLayout RadioImages, int key,
			String imgPath[])
	{
		// TODO Auto-generated constructor stub
		this.context = context;
		this.RadioImages = RadioImages;
		if (RadioImages.getChildCount() > 0)
		{
			RadioImages.removeAllViews();
		}
		this.imgPath = imgPath;
		initUI(key);
		getImages();
		getRadioImages();
		isRun();
	}

	/**
	 * 设置静态本地图片到ViewFilpper
	 * 
	 * @param id
	 */
	public void setViewFilpperImg(int[] id)
	{
		viewFlipper = new ViewFlipper(context);
		viewFlipper.setOnTouchListener(this);
		viewFlipper.setLongClickable(true);
		mGestureDetector = new GestureDetector(context, this);
		imgPath = new String[id.length];
		for (int i = 0; i < id.length; i++)
		{
			ImageView imageView = new ImageView(context);
			Bitmap bit = PhotoUtil.getBitmap(context, id[i]);
			imageView.setImageBitmap(bit);
			imageView.setScaleType(ScaleType.CENTER_CROP);
			viewFlipper.addView(imageView);
		}
	}

	public void initUI(int key)
	{
		viewFlipper = new ViewFlipper(context);
		viewFlipper.setOnTouchListener(this);
		viewFlipper.setLongClickable(true);
		imageViews = new ImageView[imgPath.length];
		fb = FinalBitmap.create(context);
		mGestureDetector = new GestureDetector(context, this);
		switch (key)
		{
		case 0:
			Home home;
			for (int i = 0; i < imgPath.length; i++)
			{
				if (datas.size() != 0)
				{
					home = (Home) datas.get(i);
					imgPath[i] = home.getImg();
				}
			}
			break;

		default:
			break;
		}
	}

	public void isRun()
	{
		if (RadioImages != null && imgPath.length > 1
				&& viewFlipper.getChildCount() > 1)
		{
			RadioImages.setVisibility(View.VISIBLE);
			thread.start();
		} else if (RadioImages != null)
		{
			RadioImages.setVisibility(View.GONE);
		}
	}

	public void getImages()
	{
		for (int i = 0; i < imageViews.length; i++)
		{
			imageView = new ImageView(context);
			imageViews[i] = imageView;
			imageViews[i].setScaleType(ScaleType.CENTER_CROP);
			Bitmap bitmap = PhotoUtil.getBitmap(context,
					R.drawable.icon_default_photo);
			fb.configLoadingImage(bitmap);
			fb.configLoadfailImage(bitmap);
			fb.configBitmapMaxHeight(350);
			fb.configBitmapMaxWidth(550);
			if (imgPath[0] == null && datas.size() == 0 && key != 5)
			{
				ImageView view = new ImageView(context);
				view.setImageResource(R.drawable.icon_default_photo);
				viewFlipper.addView(view);
			} else if (imgPath[i] == null && datas.size() == 0)
			{

			} else
			{
				fb.display(imageView, imgPath[i]);
				viewFlipper.addView(imageViews[i]);
			}
		}
	}

	public void getRadioImages()
	{
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
		params.setMargins(3, 0, 0, 0);
		RadioImg = new ImageView[imgPath.length];
		for (int i = 0; i < RadioImg.length; i++)
		{
			ImageView img = new ImageView(context);
			img.setLayoutParams(params);
			RadioImg[i] = img;
			RadioImg[i].setBackgroundResource(R.drawable.dot_carousel_focused);
			RadioImg[0].setBackgroundResource(R.drawable.dot_carousel_normal);
			RadioImages.addView(RadioImg[i]);
		}
	}

	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
			case SHOW_NEXT:
				if (showNext) 
				{
					showNextView();
				} else
				{
					showPreviousView();
				}
				break;
			}
		}
	};
	Thread thread = new Thread()
	{

		@Override
		public void run()
		{
			while (isRun)
			{
				try
				{
					Thread.sleep(1000 * 3);
					Message msg = new Message();
					msg.what = SHOW_NEXT;
					mHandler.sendMessage(msg);
				} catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
	};

	private void showNextView()
	{

		viewFlipper.setInAnimation(AnimationUtils.loadAnimation(context,
				R.anim.push_left_in));
		viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(context,
				R.anim.push_left_out));
		viewFlipper.showNext();
		currentPage++;
		if (currentPage == viewFlipper.getChildCount())
		{
			currentPage = 0;
			displayRatio_selelct(currentPage);
		} else
		{
			displayRatio_selelct(currentPage);
		}

	}

	private void showPreviousView()
	{
		viewFlipper.setInAnimation(AnimationUtils.loadAnimation(context,
				R.anim.push_right_in));
		viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(context,
				R.anim.push_right_out));
		viewFlipper.showPrevious();
		currentPage--;
		if (currentPage == -1)
		{
			currentPage = viewFlipper.getChildCount() - 1;
			displayRatio_selelct(currentPage);
		} else
		{
			displayRatio_selelct(currentPage);
		}
	}

	private void displayRatio_selelct(int id)
	{
		LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(10,
				10);
		LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(12,
				12);
		params1.setMargins(3, 0, 0, 0);
		params2.setMargins(3, 0, 0, 0);
		if (RadioImg != null)
		{
			for (int i = 0; i < RadioImg.length; i++)
			{
				RadioImg[i]
						.setBackgroundResource(R.drawable.dot_carousel_normal);
				RadioImg[id]
						.setBackgroundResource(R.drawable.dot_carousel_focused);
				RadioImg[i].setLayoutParams(params1);
				RadioImg[id].setLayoutParams(params2);
			}
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event)
	{
		return mGestureDetector.onTouchEvent(event);
	}

	@Override
	public boolean onDown(MotionEvent e)
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e)
	{
		final int id = viewFlipper.getDisplayedChild();
		return true;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY)
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY)
	{
		// TODO Auto-generated method stub
		if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
				&& Math.abs(velocityX) > FLING_MIN_VELOCITY)
		{
			showNextView();
			showNext = true;
		} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
				&& Math.abs(velocityX) > FLING_MIN_VELOCITY)
		{
			showPreviousView();
			showNext = false;
		}
		return false;
	}
}
